#!/usr/bin/env python3

from openpyxl import load_workbook
import config
import trans

if __name__ == '__main__':
    translator = trans.GoogleTranslator()
    
    workbook = load_workbook(config.input_file)
    
    sheet_names = workbook.sheetnames
    sheet_name = list(filter(lambda s: config.target_sheet_key_word in s, sheet_names))[0]
    print(sheet_name)
    sheet = workbook[sheet_name]
    
    # 遍历指定的列
    for col_letter in config.trans_col_list:
        for cell in sheet[col_letter]:
            if cell.value is not None:
                print(f"Row: {cell.row}, Column: {cell.column}, Value: {cell.value}")
                striped_string = cell.value.strip()
                if striped_string in config.skip_list:
                    continue
                elif striped_string in config.dictionary.keys():
                    trans_result = config.dictionary[striped_string]
                    print('dictionary:', trans_result)
                else:
                    try:
                        trans_result = translator.translate(striped_string, config.from_lang, config.to_lang)
                    except Exception as e:
                        print(e)
                    finally:
                        workbook.save(config.output_file)
                print(cell.value)
                print(trans_result)
                if config.append_mode is True:
                    cell.value += '\n' + trans_result
                else:
                    cell.value = trans_result
    
    workbook.save(config.output_file)
